package com.hb.trade.server.project.opening.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hb.core.pojo.enums.SectionStatusEnum;
import com.hb.core.pojo.enums.TradeWebsocketMsgEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.opening.dto.OpeningBidInfoDTO;
import com.hb.core.pojo.project.opening.dto.OpeningBidTenderDTO;
import com.hb.core.pojo.project.opening.dto.OpeningBidTenderRecordDTO;
import com.hb.core.pojo.project.opening.entity.OpeningBidInfo;
import com.hb.core.pojo.project.opening.entity.OpeningBidTender;
import com.hb.core.pojo.project.opening.entity.OpeningBidTenderRecord;
import com.hb.core.pojo.project.opening.entity.OpeningSet;
import com.hb.core.pojo.project.opening.mapper.OpeningBidTenderMapper;
import com.hb.core.pojo.project.opening.mapper.OpeningBidTenderRecordMapper;
import com.hb.core.pojo.project.proj.entity.AbstractProject;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.core.pojo.project.section.entity.BidSectionTender;
import com.hb.trade.server.project.opening.service.OpeningBidHallService;
import com.hb.trade.server.project.opening.service.OpeningBidTenderRecordService;
import com.hb.trade.server.project.opening.service.OpeningBidTenderService;
import com.hb.trade.server.project.opening.service.OpeningSetService;
import com.hb.trade.server.project.proj.service.ProjectService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import com.hb.trade.server.project.section.service.raw.BidSectionTenderService;
import com.hb.trade.server.project.util.DateUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.temporal.TemporalUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 开标大厅控制器
 *
 * @author lixiang
 * @date 2021-11-23 11:11:42
 */
@RestController
public class BidOpeningHallController {

    protected final ProjectService projectService;
    protected final BidSectionService bidSectionService;
    protected final OpeningSetService openingSetService;
    protected final OpeningBidHallService openingBidHallService;
    protected final OpeningBidTenderService openingBidTenderService;
    protected final BidSectionTenderService bidSectionTenderService;
    protected final OpeningBidTenderRecordService openingBidTenderRecordService;

    public BidOpeningHallController(ProjectService projectService,
                                    BidSectionService bidSectionService,
                                    OpeningSetService openingSetService,
                                    OpeningBidHallService openingBidHallService,
                                    OpeningBidTenderService openingBidTenderService,
                                    BidSectionTenderService bidSectionTenderService,
                                    OpeningBidTenderRecordService openingBidTenderRecordService) {
        this.projectService = projectService;
        this.bidSectionService = bidSectionService;
        this.openingSetService = openingSetService;
        this.openingBidHallService = openingBidHallService;
        this.openingBidTenderService = openingBidTenderService;
        this.bidSectionTenderService = bidSectionTenderService;
        this.openingBidTenderRecordService = openingBidTenderRecordService;
    }

    /**
     * 根据项目Id查询今日开标标段分页
     *
     * @param bidSectionDTO
     * @return
     */
    @PostMapping("/hall/pageTodayBidOpeningSection")
    public Message<PageResult> pageTodayBidOpeningSection(@RequestBody BidSectionDTO bidSectionDTO) {
        PageResult pageResult = openingBidHallService.pageTodayBidOpeningSection(bidSectionDTO);
        return MsgUtil.success(pageResult);
    }
   /**
     * 根据项目Id查询今日开标标段分页(不带分页)
     *
     * @param bidSectionDTO
     * @return
     */
    @PostMapping("/hall/listTodayBidOpeningSection")
    public Message<List<BidSectionDTO>> listTodayBidOpeningSection(@RequestBody BidSectionDTO bidSectionDTO) {
        List<BidSectionDTO> bidSectionDTOList = openingBidHallService.listTodayBidOpeningSection(bidSectionDTO);
        return MsgUtil.success(bidSectionDTOList);
    }

    /**
     * 根据企业ID和日期查询当日开标标段
     * @param bidSectionDTO
     * @return
     * @author ljs
     */
    @PostMapping("/hall/pageHomeTodayOpeningSection")
    public Message<PageResult> pageHomeTodayOpeningSection(@RequestBody BidSectionDTO bidSectionDTO) {
        PageResult pageResult = openingBidHallService.pageTodayOpeningSection(bidSectionDTO);
        return MsgUtil.success(pageResult);
    }
    /**
     * 根据标段Id查询开标记录表数据
     *      (如果是第一次进入开标大厅，则新增一条记录)
     *
     * @param openingBidInfo
     * @return
     */
    @PostMapping("/hall/getOpeningBidInfoBySectionId")
    public Message<OpeningBidInfoDTO> getOpeningBidInfoBySectionId(@RequestBody OpeningBidInfo openingBidInfo) {
        Long sectionId = openingBidInfo.getSectionId();
        if (null == sectionId) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询标段相关属性
        BidSection bidSection = bidSectionService.getById(sectionId);
        if (null == bidSection) {
            return MsgUtil.fail("获取开标记录数据失败，未查询到相关标段信息");
        }
        //查询开标设置相关属性
        OpeningSet openingSet = openingSetService.getById(bidSection.getOpeningSetId());
        if (null == openingSet) {
            return MsgUtil.fail("获取开标记录数据失败，未查询到相关开标设置信息");
        }
        //根据标段Id查询开标记录
        OpeningBidInfo openingBidInfoEntity = openingBidHallService.getOne(new LambdaQueryWrapper<OpeningBidInfo>()
                .eq(OpeningBidInfo::getSectionId, sectionId)
                .last("limit 1"));
        if (null == openingBidInfoEntity) {
            //判断如果开标记录数据为空，则是第一次进入开标大厅，新增一条开标记录数据
            openingBidInfoEntity = new OpeningBidInfo();
            openingBidInfoEntity.buildBaseData(1);
            openingBidInfoEntity.setOpeningSetId(openingSet.getId());
            openingBidInfoEntity.setIsEndSign(1);
            openingBidInfoEntity.setSignWay(openingSet.getConfirmMethod());
            openingBidInfoEntity.setSectionId(bidSection.getId());
            openingBidInfoEntity.setOpenBidTime(bidSection.getOpenBidTime());
            //查询投标人递交标书数量
            Long tenderCount = bidSectionTenderService.count(new LambdaQueryWrapper<BidSectionTender>()
                    .eq(BidSectionTender::getSectionId, sectionId)
                    .eq(BidSectionTender::getStatus, 1));
            openingBidInfoEntity.setBidSubmitCount(new Long(tenderCount).intValue());
            //首次进入开标大厅，设置状态为签到中
            openingBidInfoEntity.setStatus(1);
            openingBidHallService.save(openingBidInfoEntity);
        }
        //转换DTO
        OpeningBidInfoDTO openingBidInfoDTO = openingBidInfoEntity.to(OpeningBidInfoDTO.class);
        openingBidInfoDTO.setDecryptionTime(openingSet.getDecryptionTime());
        openingBidInfoDTO.setSignTime(openingSet.getSignTime());
        //查询项目信息，获取招标方及代理机构信息
        AbstractProject project = projectService.getProjectBySectionId(openingBidInfo.getSectionId());
        openingBidInfoDTO.setSupervisorName(openingSet.getSupervisorName());
        openingBidInfoDTO.setHostName(openingSet.getHostName());
        openingBidInfoDTO.setBiddingOrganizeInfo(project.getBiddingOrganizeInfo());
        openingBidInfoDTO.setAgencyOrganizeInfo(project.getAgencyOrganizeInfo());
        // 根据开标状态获取枚举类实例
        TradeWebsocketMsgEnum insByOpeningStatus = TradeWebsocketMsgEnum.getInsByOpeningStatus(openingBidInfoDTO.getStatus(), openingBidInfoDTO.getIsEndSign());
        if(null != insByOpeningStatus){
            openingBidInfoDTO.setInfoType(insByOpeningStatus.getCode());
            openingBidInfoDTO.setTypeName(insByOpeningStatus.getShowName());
        }
        openingBidInfoDTO.setDecryptEndTime(openingBidInfoEntity.getDecryptEndTime());
        openingBidInfoDTO.setSignEndTime(openingBidInfoEntity.getSignEndTime());
        return MsgUtil.success(openingBidInfoDTO);
    }

    /**
     * 统计大厅各阶段人数（总投标人数、有效投标人数、无效投标人数、已解密人数、已签名人数）
     *
     * @param openingBidInfo  记录表主键Id
     * @return
     */
    @PostMapping("/hall/countHallInfo")
    public Message<Map> countHallInfo(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        Map count = openingBidHallService.countHallInfo(openingBidInfo.getId());
        return MsgUtil.success(count);
    }

    /**
     * 获取开标大厅实时记录数据(招标方)
     *
     * @param openingBidTender
     * @return
     */
    @PostMapping("/hall/listBidOpeningAllRecord")
    public Message<List<OpeningBidTenderRecordDTO>> listBidOpeningAllRecord(@RequestBody OpeningBidTender openingBidTender) {
        if (null == openingBidTender.getOpeningBidInfoId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        List<OpeningBidTenderRecordDTO> tenderRecordDTOList = openingBidHallService.listBidOpeningRecord(openingBidTender);
        return MsgUtil.success(tenderRecordDTOList);
    }

    /**
     * 获取开标大厅实时记录数据(投标方)
     *
     * @param openingBidTender
     * @return
     */
    @PostMapping("/hall/listBidOpeningRecord")
    public Message<List<OpeningBidTenderRecordDTO>> listBidOpeningRecord(@RequestBody OpeningBidTender openingBidTender) {
        if (null == openingBidTender.getOpeningBidInfoId() || null == openingBidTender.getBidderId()) {
            return MsgUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        List<OpeningBidTenderRecordDTO> tenderRecordDTOList = openingBidHallService.listBidOpeningRecord(openingBidTender);
        return MsgUtil.success(tenderRecordDTOList);
    }

    /**
     * 获取开标大厅投标方状态数据
     *
     * @param openingBidTender
     * @return
     */
    @PostMapping("/hall/listBidOpeningTender")
    public Message<List<OpeningBidTenderDTO>> listBidOpeningTender(@RequestBody OpeningBidTender openingBidTender) {
        if (null == openingBidTender.getOpeningBidInfoId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        List<OpeningBidTenderDTO> tenderRecordDTOList = openingBidHallService.listBidOpeningTender(openingBidTender);
        return MsgUtil.success(tenderRecordDTOList);
    }

    /**
     * 开始签到
     * @deprecated 需求变更，取消开启签到按钮功能，提前30分钟可进入开标大厅直接进行签到
     * @param openingBidInfo
     * @return
     */
    @Deprecated
    @PostMapping("/hall/startSignin")
    public Message<OpeningBidInfo> startSignin(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 0) {
            return MsgUtil.fail("操作失败，当前状态不允许签到");
        }
        //设置为签到
        openingBidInfo.setStatus(1);
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 开始开标
     *
     * @return
     */
    @PostMapping("/hall/startBidOpening")
    @Transactional(rollbackFor = Exception.class)
    public Message<OpeningBidInfo> startBidOpening(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 1) {
            return MsgUtil.fail("操作失败，当前状态不允许开标");
        }
        LocalDateTime openBidTime = openingBidInfo1.getOpenBidTime();
        if(null != openBidTime && LocalDateTime.now().isBefore(openBidTime)) {
            return MsgUtil.fail("还未到达开标时间，请勿操作");
        }
        //修改开标记录表状态
        openingBidInfo.setStatus(2);
        openingBidInfo.setIsStart(1);
        openingBidInfo.setOpenBidTime(LocalDateTime.now());
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        //修改标段表section_status属性为开标开始
        BidSection bidSection = new BidSection();
        bidSection.setId(openingBidInfo1.getSectionId());
        bidSection.setSectionStatus(SectionStatusEnum.BID_OPENING.getId());
        bidSectionService.updateById(bidSection);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 开始解密
     *
     * @return
     */
    @PostMapping("/hall/startDecrypt")
    public Message<OpeningBidInfo> startDecrypt(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 2) {
            return MsgUtil.fail("操作失败，当前状态不允许开启解密");
        }
        //查询开标设置相关属性
        OpeningSet openingSet = openingSetService.getById(openingBidInfo1.getOpeningSetId());
        if (null == openingSet) {
            return MsgUtil.fail("获取开标记录数据失败，未查询到相关开标设置信息");
        }

        openingBidInfo.setStatus(3);
        openingBidInfo.setDecryptStartTime(LocalDateTime.now());
        openingBidInfo.setDecryptEndTime(openingBidInfo.getDecryptStartTime().plusMinutes(openingSet.getDecryptionTime()));
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 结束解密
     *
     * @return
     */
    @PostMapping("/hall/endDecrypt")
    public Message<OpeningBidInfo> endDecrypt(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 3) {
            return MsgUtil.fail("操作失败，当前状态不允许结束解密");
        }
        openingBidInfo.setStatus(4);
        openingBidInfo.setDecryptEndTime(LocalDateTime.now());
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 开始唱标
     *
     * @return
     */
    @PostMapping("/hall/startSing")
    public Message<OpeningBidInfo> startSing(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        /*if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 4) {
            return MsgUtil.fail("操作失败，当前状态不允许唱标");
        }*/
        openingBidInfo.setStatus(5);
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        openingBidHallService.generateSingAudio(openingBidInfo1);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 开始确认
     *
     * @return
     */
    @PostMapping("/hall/startConfirm")
    public Message<OpeningBidInfo> startConfirm(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 5) {
            return MsgUtil.fail("操作失败，当前状态不允许开启确认");
        }
        //查询开标设置相关属性
        OpeningSet openingSet = openingSetService.getById(openingBidInfo1.getOpeningSetId());
        if (null == openingSet) {
            return MsgUtil.fail("获取开标记录数据失败，未查询到相关开标设置信息");
        }

        openingBidInfo.setStatus(6);
        openingBidInfo.setIsEndSign(1);
        openingBidInfo.setSignStartTime(LocalDateTime.now());
        openingBidInfo.setSignEndTime(openingBidInfo.getSignStartTime().plusMinutes(openingSet.getSignTime()));

        Boolean result = openingBidHallService.updateById(openingBidInfo);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 结束确认
     *
     * @return
     */
    @PostMapping("/hall/endConfirm")
    public Message<OpeningBidInfo> endConfirm(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 6) {
            return MsgUtil.fail("操作失败，当前状态不允许结束确认");
        }
        openingBidInfo.setStatus(6);
        openingBidInfo.setIsEndSign(2);
        openingBidInfo.setSignStartTime(LocalDateTime.now());
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 开标结束
     *
     * @return
     */
    @PostMapping("/hall/endBidOpening")
    @Transactional(rollbackFor = Exception.class)
    public Message<OpeningBidInfo> endBidOpening(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询库中存在的记录，判断状态是否允许操作
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        /*if (null != openingBidInfo1 && openingBidInfo1.getStatus() != 6) {
            return MsgUtil.fail("操作失败，当前状态不允许结束开标");
        }*/
        //修改开标记录表状态
        openingBidInfo.setStatus(7);
        openingBidInfo.setIsEndSign(2);
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        //修改标段表section_status属性为开标结束
        BidSection bidSection = new BidSection();
        bidSection.setId(openingBidInfo1.getSectionId());
        bidSection.setSectionStatus(SectionStatusEnum.BID_ASSESSING.getId());
        bidSectionService.updateById(bidSection);
        //删除唱标词音频文件
        FileUtil.del("hall_audio/" + openingBidInfo1.getSectionId() + ".mp3");
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 设置项目流标
     *
     * @param openingBidInfo
     * @return
     */
    @PostMapping("/hall/setFlowBid")
    @Transactional(rollbackFor = Exception.class)
    public Message<OpeningBidInfo> setFlowBid(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //设置为流标
        openingBidInfo.setStatus(8);
        Boolean result = openingBidHallService.updateById(openingBidInfo);
        //设置标段状态为流标
        OpeningBidInfo open = openingBidHallService.getById(openingBidInfo.getId());
        BidSection bidSection = new BidSection();
        bidSection.setId(open.getSectionId());
        bidSection.setFailFlag(1);
        bidSectionService.updateById(bidSection);
        return MsgUtil.success(openingBidInfo);
    }

    /**
     * 查看开标记录
     *
     * @param openingBidInfo
     * @return
     */
    @PostMapping("/hall/getOpeningBidInfo")
    public Message<OpeningBidInfo> getOpeningBidInfo(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        OpeningBidInfo openingBidInfo1 = openingBidHallService.getById(openingBidInfo.getId());
        return MsgUtil.success(openingBidInfo1);
    }

    /**
     * 查看开标一览表
     *
     * @param openingBidInfo
     * @return
     */
    @PostMapping("/hall/viewBidOpeningSchedule")
    public Message<List<OpeningBidTender>> viewBidOpeningSchedule(@RequestBody OpeningBidInfo openingBidInfo) {
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        List<OpeningBidTender> openingBidTenderList = openingBidHallService.listBidOpeningSchedule(openingBidInfo);
        return MsgUtil.success(openingBidTenderList);
    }
    /**
     *
     * @Description:更新开标一览表表内容
     * @Author: zhongdl
     * @Date:2022/5/17 17:09
     * @param openingBidInfo:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/hall/updateBidOpeningSchedule")
    public Message<Boolean> updateBidOpeningSchedule(@RequestBody OpeningBidInfo openingBidInfo){
        if (null == openingBidInfo.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        boolean update = openingBidHallService.updateById(openingBidInfo);
        return MsgUtil.success(update);
    }


    //------------------------------------------  投标方端接口  ------------------------------------------

    /**
     * 根据开标记录Id与投标人Id查询投标方信息
     *
     * @param openingBidTender
     * @return
     */
    @PostMapping("/hall/getOpeningBidTender")
    public Message<OpeningBidTender> getOpeningBidTender(@RequestBody OpeningBidTender openingBidTender) {
        if (null == openingBidTender.getOpeningBidInfoId() || null == openingBidTender.getBidderId()) {
            return MsgUtil.fail("获取投标方开标大厅记录失败，参数为空");
        }
        OpeningBidTender openingBidTenderInfo = openingBidTenderService.getOne(new LambdaQueryWrapper<OpeningBidTender>()
                .eq(OpeningBidTender::getOpeningBidInfoId, openingBidTender.getOpeningBidInfoId())
                .eq(OpeningBidTender::getBidderId, openingBidTender.getBidderId())
                .last("limit 1"));
        if (null == openingBidTenderInfo) {
            //判断如果为空，则新增一条数据
            openingBidTenderInfo = new OpeningBidTender();
            openingBidTenderInfo.buildBaseData(1);
            //查询开标记录表数据
            OpeningBidInfo openingBidInfo = openingBidHallService.getById(openingBidTender.getOpeningBidInfoId());
            if (null == openingBidInfo) {
                return MsgUtil.fail("获取投标方开标大厅记录失败，未获取到相关开标记录信息");
            }
            //查询当前投标人投标信息，判断是否递交投标文件
            BidSectionTender bidSectionTender = bidSectionTenderService.getOne(new LambdaQueryWrapper<BidSectionTender>()
                    .eq(BidSectionTender::getSectionId, openingBidInfo.getSectionId())
                    .eq(BidSectionTender::getBidderId, openingBidTender.getBidderId())
                    .last("limit 1"));
            if (null == bidSectionTender) {
                return MsgUtil.fail("未获取到当前企业投标信息");
            }
            if (bidSectionTender.getStatus() != 1) {
                return MsgUtil.fail("当前企业还未递交投标文件");
            }
            openingBidTenderInfo.setOpeningBidInfoId(openingBidInfo.getId());
            openingBidTenderInfo.setSectionId(openingBidTender.getSectionId());
            openingBidTenderInfo.setBidderId(openingBidTender.getBidderId());
            openingBidTenderInfo.setOrgId(openingBidTender.getOrgId());
            openingBidTenderInfo.setSignFlag(2);
            openingBidTenderInfo.setDecryptFlag(2);
            openingBidTenderInfo.setConfirmFlag(2);
            openingBidTenderService.save(openingBidTenderInfo);
        }
        return MsgUtil.success(openingBidTenderInfo);
    }

    /**
     * 投标人签到
     *
     * @param openingBidTender
     * @return
     */
    @PostMapping("/hall/bidderSignin")
    @Transactional(rollbackFor = Exception.class)
    public Message bidderSignin(@RequestBody OpeningBidTender openingBidTender) {
        if (null == openingBidTender.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询当前投标人是否已经签到
        OpeningBidTender openingBidTender1 = openingBidTenderService.getById(openingBidTender.getId());
        if (null != openingBidTender1 && openingBidTender1.getSignFlag() == 1) {
            return MsgUtil.fail("当前状态不允许继续签到");
        }
        openingBidTender.setSignFlag(1);
        openingBidTender.setUpdateTime(LocalDateTime.now());
        //向记录表插入条记录
        OpeningBidTenderRecord openingBidTenderRecord = new OpeningBidTenderRecord();
        openingBidTenderRecord.buildBaseData(1);
        openingBidTenderRecord.setTenderId(openingBidTender.getId());
        openingBidTenderRecord.setActionType(1);
        openingBidTenderRecord.setActionTime(LocalDateTime.now());
        openingBidTenderRecordService.save(openingBidTenderRecord);
        Boolean result = openingBidTenderService.updateById(openingBidTender);
        return result ? MsgUtil.success(openingBidTender) : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }

    /**
     * 投标人解密
     *
     * @param openingBidTender
     * @return
     */
    @PostMapping("/hall/bidderDecrypt")
    @Transactional(rollbackFor = Exception.class)
    public Message bidderDecrypt(@RequestBody OpeningBidTender openingBidTender) {
        if (null == openingBidTender.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询当前投标人是否已经解密
        OpeningBidTender openingBidTender1 = openingBidTenderService.getById(openingBidTender.getId());
        if (null != openingBidTender1 && openingBidTender1.getDecryptFlag() == 1) {
            return MsgUtil.fail("当前状态不允许继续解密");
        }
        openingBidTender.setDecryptFlag(1);
        openingBidTender.setUpdateTime(LocalDateTime.now());
        //向记录表插入条记录
        OpeningBidTenderRecord openingBidTenderRecord = new OpeningBidTenderRecord();
        openingBidTenderRecord.buildBaseData(1);
        openingBidTenderRecord.setTenderId(openingBidTender.getId());
        openingBidTenderRecord.setActionType(2);
        openingBidTenderRecord.setActionTime(LocalDateTime.now());
        openingBidTenderRecordService.save(openingBidTenderRecord);
        Boolean result = openingBidTenderService.updateById(openingBidTender);
        return result ? MsgUtil.success(openingBidTender) : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }

    /**
     * 投标人确认
     *
     * @param openingBidTender
     * @return
     */
    @PostMapping("/hall/bidderComfirm")
    @Transactional(rollbackFor = Exception.class)
    public Message bidderComfirm(@RequestBody OpeningBidTender openingBidTender) {
        if (null == openingBidTender.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //查询当前投标人是否已经确认
        OpeningBidTender openingBidTender1 = openingBidTenderService.getById(openingBidTender.getId());
        if (null != openingBidTender1 && openingBidTender1.getConfirmFlag() == 1) {
            return MsgUtil.fail("当前状态不允许继续确认");
        }
        openingBidTender.setConfirmFlag(1);
        openingBidTender.setUpdateTime(LocalDateTime.now());
        //向记录表插入条记录
        OpeningBidTenderRecord openingBidTenderRecord = new OpeningBidTenderRecord();
        openingBidTenderRecord.buildBaseData(1);
        openingBidTenderRecord.setTenderId(openingBidTender.getId());
        openingBidTenderRecord.setActionType(3);
        openingBidTenderRecord.setActionTime(LocalDateTime.now());
        openingBidTenderRecordService.save(openingBidTenderRecord);
        Boolean result = openingBidTenderService.updateById(openingBidTender);
        return result ? MsgUtil.success(openingBidTender) : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }
    /**
     *
     * @Description:查询投标人标段的解密记录
     * @Author: zhongdl
     * @Date:2022/2/24 9:02
     * @param openingBidTender:
     * @return: {@link Message< OpeningBidTender> }
     **/
    @PostMapping("/openingBidTender/getOpeningBidTenderBySection")
    public Message<OpeningBidTender> getOpeningBidTenderBySection(@RequestBody OpeningBidTender openingBidTender){

        OpeningBidTender bidTender = openingBidTenderService.getOpeningBidTenderBySection(openingBidTender);
        return MsgUtil.success(bidTender);
    }
}
